﻿unit ETypeFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JvFormPlacement, JvComponentBase, JvAppStorage,
  cxGridExportLink,
  dxRibbonForm,
  JvAppIniStorage, JvXPBar, JvExControls, JvXPCore, JvXPContainer, GridsEh,
  DBGridEh, ComCtrls, JvExComCtrls, JvComCtrls, ExtCtrls, JvExExtCtrls,
  JvExtComponent, JvSplit, DB, JvXPButtons, Grids, DBGrids, FIBSQLMonitor,
  StdCtrls, FIBDataSet, pFIBDataSet, CheckLst, DBCtrls, pFIBCacheQueries,
  FIBQuery;

type
  TETypeForm = class(Tdxcustomribbonform)
    Panel2: TPanel;
    JvAppIniFileStorage1: TJvAppIniFileStorage;
    JvFormStorage1: TJvFormStorage;
    Panel1: TPanel;
    Splitter1: TSplitter;
    Panel4: TPanel;
    Panel5: TPanel;
    Panel6: TPanel;
    Panel7: TPanel;
    Grid1: TDBGridEh;
    DS1: TpFIBDataSet;
    Source1: TDataSource;
    Splitter2: TSplitter;
    DBNavigator1: TDBNavigator;
    Grants: TCheckListBox;
    Panel8: TPanel;
    Grid: TDBGridEh;
    DS2: TpFIBDataSet;
    Source2: TDataSource;
    DBNavigator2: TDBNavigator;
    ENext: TCheckListBox;
    GroupBox2: TGroupBox;
    Shb: TComboBox;
    GroupBox1: TGroupBox;
    Derect: TComboBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure GrantsClickCheck(Sender: TObject);
    procedure Source1DataChange(Sender: TObject; Field: TField);
    procedure ENextClickCheck(Sender: TObject);
    procedure Source2DataChange(Sender: TObject; Field: TField);
    procedure DS1AfterPost(DataSet: TDataSet);
    procedure ShbChange(Sender: TObject);
    procedure DerectChange(Sender: TObject);
    procedure Grid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormActivate(Sender: TObject);
  private
    SrcUpdate: boolean;
    procedure SetCheck(S: string);
    function GetCheck: string;
    procedure FreeENext;
    procedure MakeENext;
    procedure ClearENext;
    procedure SetENext;
    procedure GetENext;
    procedure EditRecord;
  public
    { Public declarations }
  end;


  TECode = class(TObject)
   private
    fCode: string;
   public
    property Code: string read fCode write fCode;
  end;

var
  ETypeForm: TETypeForm;

implementation
 uses  SetGridEh, {GridToXLS,} ETypeEditFrm, DModule, cmUtils;

{$R *.dfm}

{ TETypeFormObject }

procedure TETypeForm.FormActivate(Sender: TObject);
begin
  SetTabActive('spr',True);
end;

procedure TETypeForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 RemoveBtn(Name);
 SaveGrids(Self);
 Action:=caFree;
end;

procedure TETypeForm.FormCreate(Sender: TObject);
begin
 AddButton(self);
 InitGrids(Self);
 MakeENext;
end;

procedure TETypeForm.FormResize(Sender: TObject);
begin
 Panel7.Width:=Width div 2;
end;

function TETypeForm.GetCheck: string;
 var i: integer;
begin
 Result:='';
 for i:=0 to Grants.Items.Count - 1 do
  if Grants.Checked[i] then Result:=Result+'1'
                       else Result:=Result+'0';
end;

procedure TETypeForm.GrantsClickCheck(Sender: TObject);
 var IsEdit: boolean;
begin
 if not DS1.IsEmpty then
   begin
    IsEdit:=(DS1.State in [dsEdit, dsInsert]);
    if not IsEdit then Exit;
    DS1.FieldByName('Owner').AsString:=GetCheck;
   end;
end;

procedure TETypeForm.EditRecord;
 var Form: TETypeEditForm;
begin
 if DS1.IsEmpty then Exit;
 Form:=TETypeEditForm.Create(Application);
 Form.ShowModal;
 Form.Free;
 DS1.Prior;
end;

procedure TETypeForm.Grid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
 case Key of
  VK_RETURN: EditRecord;
 end;
end;

procedure TETypeForm.SetCheck(S: string);
 var i: integer;
begin
 for i:=0 to Grants.Items.Count - 1 do
  if Copy(S,i+1,1)='1' then Grants.Checked[i]:=true
                       else Grants.Checked[i]:=false;
end;

procedure TETypeForm.Source1DataChange(Sender: TObject; Field: TField);
begin
 SrcUpdate:=true;
 SetCheck(DS1.FieldByName('Owner').AsString);
 Grants.Enabled:=(DS1.State in [dsEdit, dsInsert]);
 Derect.ItemIndex:=DS1.FieldByName('Derect').AsInteger;
 Derect.Enabled:=Grants.Enabled;
 SrcUpdate:=false;
end;

//-------------------работа со списком ENext-----------------------------------
procedure TETypeForm.FreeENext;
 var i: integer;
begin
 for i:=0 to ENext.Items.Count - 1 do ENext.Items.Objects[i].Free;
 ENext.Items.Clear;
end;

procedure TETypeForm.MakeENext;
 var Q: TFIBQuery;
     Obj: TECode;
begin
 FreeENext;
 Q:=GetQueryForUse(dm1.trnRD, 'SELECT * FROM spr$EType ORDER BY Name');
 Q.ExecQuery;
 while not Q.Eof do
  begin
   Obj:=TECode.Create;
   Obj.Code:=Q.FN('Code').AsString;
   ENext.AddItem(Q.FN('Name').AsString, Obj);
   Q.Next;
  end;
 FreeQueryForUse(Q); 
end;

procedure TETypeForm.ClearENext;
 var i: integer;
begin
 for i:=0 to ENext.Items.Count - 1 do ENext.Checked[i]:=false;
end;

procedure TETypeForm.SetENext;
 var S: string;
     i: integer;
begin
 ClearENext;
 if DS2.IsEmpty then Exit;
 S:=DS2.FN('ENext').AsString;
 for i:=0 to ENext.Items.Count - 1 do
  ENext.Checked[i]:=Pos(';'+TECode(ENext.Items.Objects[i]).Code+';',S)<>0;
end;

procedure TETypeForm.GetENext;
 var S: string;
     i: integer;
begin
 S:=';';
 for i:=0 to ENext.Count - 1 do
  if ENext.Checked[i] then
    S:=S+TECode(ENext.Items.Objects[i]).Code+';';
 DS2.FN('ENext').AsString:=S;
end;

procedure TETypeForm.DerectChange(Sender: TObject);
begin
 if SrcUpdate or not Derect.Enabled or DS1.IsEmpty then Exit;
 DS1.Edit;
 DS1.FN('Derect').AsInteger:=Derect.ItemIndex;
 DS1.Post;
end;

procedure TETypeForm.DS1AfterPost(DataSet: TDataSet);
begin
 MakeENext;
end;

procedure TETypeForm.ENextClickCheck(Sender: TObject);
begin
 GetENext;
end;

procedure TETypeForm.Source2DataChange(Sender: TObject; Field: TField);
begin
 SetENext;
 ENext.Enabled:=(DS2.State in [dsEdit, dsInsert]);
end;

//--------------------фильтр по профилю-----------------------------------------
procedure TETypeForm.ShbChange(Sender: TObject);
 var S: string;
     i: integer;
begin
 DS1.Close;
 DS1.CancelConditions;
 DS1.Conditions.Clear;
 if Shb.ItemIndex>0 then
   begin
    S:='';
    for i:=2 to Shb.ItemIndex do S:=S+'_';
    S:='Owner LIKE '#39+S+'1%'#39;
    DS1.Conditions.AddCondition('Shb',S, true);
   end;
 DS1.ApplyConditions(true);
end;

end.
